package com.atguigu.redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author layne
 */
public class JedisPoolTest {
    public static void main(String[] args) {
        //0 设置连接池
        JedisPoolConfig jedisPoolConfig =new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(10); //最大可用连接数
        jedisPoolConfig.setMaxIdle(5); //最大闲置连接数
        jedisPoolConfig.setMinIdle(5); //最小闲置连接数
        jedisPoolConfig.setBlockWhenExhausted(true); //连接耗尽是否等待
        jedisPoolConfig.setMaxWaitMillis(2000); //等待时间
        jedisPoolConfig.setTestOnBorrow(true); //取连接的时候进行一下测试 ping pong

        //1 创建连接池
        JedisPool jedisPool = new JedisPool(jedisPoolConfig,"hadoop102", 6379);

        //2 使用连接池获取客户端
        Jedis jedis = jedisPool.getResource();

        //3 使用客户端
        //查看所有key
        System.out.println(jedis.keys("*"));
        //String
        jedis.set("idea","0926");
        System.out.println(jedis.keys("*"));
        System.out.println(jedis.get("idea"));

        //list
        jedis.rpush("l2l","a","b","c","d","e");
        System.out.println(jedis.keys("*"));
        System.out.println(jedis.lrange("l2l", 0, -1));

        //set
        jedis.sadd("s2s","a","b","c","d","e","a","b","c","d","e");
        System.out.println(jedis.keys("*"));
        System.out.println(jedis.smembers("s2s"));

        //hash
        jedis.hset("h2h","name","zs");
        jedis.hset("h2h","age","18");
        jedis.hset("h2h","sex","man");
        System.out.println(jedis.keys("*"));
        System.out.println(jedis.hgetAll("h2h"));

        //4 关闭客户端
        jedis.close();

        //5 关闭连接池
        jedisPool.close();
    }
}
